package com.srwl.mytx.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.srwl.mytx.utils.DateUtils;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.AdvertisementDetailActivity;
import com.srwl.mytx.activity.ConversationActivity.AdvertisementNotifyActivity;
import com.srwl.mytx.activity.SetAdvertisementContentActivity;
import com.srwl.mytx.domain.Advertisement;
import com.srwl.mytx.domain.Message;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AdvertisementNotifyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Message> messageList;
    private NotifyMessageInteractionListener listener;


    public AdvertisementNotifyAdapter(Context context, List<Message> messageList, NotifyMessageInteractionListener listener) {
        this.context = context;
        this.messageList = messageList;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return messageList.get(position).getSubType();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.advertisement_notify_message_item, parent, false);
        return new SystemMsgViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        SystemMsgViewHolder viewHolder = (SystemMsgViewHolder) holder;
        final Message notify = messageList.get(position);
        viewHolder.tv_title.setText(notify.getTitle());
        viewHolder.tv_date.setText(DateUtils.getTimestampString(notify.getCreated()));
        Integer subType = notify.getSubType();
        String content = "";
        switch (subType) {

            case Constant.PUSH_MESSAGE_ADVERTISEMENT_DEPOSIT_NOT_ENOUGH: {
                Advertisement advertisement = JSONObject.parseObject(notify.getExtraData(), Advertisement.class);
                if (advertisement == null) {
                    break;
                }
                if (advertisement.getAmount().compareTo(advertisement.getDeposit()) > 0) {
                    content = "您投放在[ " + advertisement.getTitle() + " ]的广告位保证金不足，将不再参加后续时段的竞价投放， 点击这儿去设置";
                } else {
                    content = "您投放在[ " + advertisement.getTitle() + " ]的广告位保证金已补足，将继续参与竞价投放";
                }
                viewHolder.tv_content.setText(content);
                //用传广告位id 的方式打开广告详情，会重新去后端加载广告信息，避免信息过时。
                viewHolder.itemView.setOnClickListener(v -> AdvertisementDetailActivity.startActivity((context), advertisement.getId()));
            }
            break;

            case Constant.PUSH_MESSAGE_ADVERTISEMENT_NEED_SET_SHOW_CONTENT:
                Advertisement advertisement = JSONObject.parseObject(notify.getExtraData(), Advertisement.class);
                if (advertisement == null) {
                    break;
                }
                if (TextUtils.isEmpty(advertisement.getVideo()) && TextUtils.isEmpty(advertisement.getPicture())) {
                    content = "您投放在[ " + advertisement.getTitle() + " ]的广告位展示内容未设置， 点击这儿去设置";
                } else {
                    content = "您投放在[ " + advertisement.getTitle() + " ]的广告位展示内容已经设置";
                }
                viewHolder.tv_content.setText(content);
                viewHolder.itemView.setOnClickListener(v -> ((Activity) context).startActivityForResult(new Intent(context, SetAdvertisementContentActivity.class).putExtra("advertisement", advertisement), AdvertisementNotifyActivity.REQUEST_CODE_TO_SET_ADVERTISEMENT));

                break;
        }

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                listener.onItemLongClick(notify);
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        return messageList.size();
    }


    public class SystemMsgViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.tv_date)
        TextView tv_date;

        public SystemMsgViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

}
